package com.myblog.adkblog.enums;

import com.myblog.adkblog.service.Impl.ArticleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.function.Function;


public enum DataFetcherStrategyEnum implements DataFetcherStrategy{
    ORDER("119",param -> DataServiceHolder.SERVICE.getIndexBanner());

    private final String clientId;
    private final Function<String,Object> function;

    DataFetcherStrategyEnum(String clientId, Function<String, Object> function) {
        this.clientId = clientId;
        this.function = function;
    }
    public static DataFetcherStrategyEnum fuzzyFetcherStrategyEnum(String clientId){
        for (DataFetcherStrategyEnum value : DataFetcherStrategyEnum.values()) {
            if(value.clientId.equals(clientId)){
                return value;
            }
        }
        throw new RuntimeException("找不到对应的客户端");
    }
    @Component
    public static class DataServiceHolder{

        private static ArticleServiceImpl SERVICE;

        @Autowired
        public DataServiceHolder(ArticleServiceImpl articleService) {
            SERVICE = articleService;
        }
    }
    @Override
    public Object fetchData(String clientId) {
        return function.apply(clientId);
    }
}
